// C1 E18 Edition2
// Add: Count how many ' ' and '\t'in the trail.
// This edtion has an error in use of while in line31,33,44,46. Modify in dition 3.

#include <stdio.h>

#define MAXLINE 1000
#define LONGS 80

int getstr(char s[], int lim);
// void copystr(char source[], char dest[]);

int main()
{
    int len;
    int i;
    int nb, nt;
    char s[MAXLINE];

    len = 0;

    while ((len = getstr(s, MAXLINE)) > 0)
    {
        nb = nt = 0;
        i = len;

        if (s[i - 1] == '\n') // Case 1: have'\n'
        {
            printf("\n>>>Have '\\n' in the trail.\n");
            i--;
            while (s[i - 1] == ' ') //ERROR: this is wrong. Please use "xubb\t' '\t" to test.
                s[i - 1] = '\n', s[i] = '\0', i--, nb++;
            while (s[i - 1] == '\t')
                s[i - 1] = '\n', s[i] = '\0', i--, nt++;
            i++;
            if (i == 1)
                printf(">>>Blank, delete.\n");
            else
                printf(">>>Source = %3d\t\tNow = %3d\tDelete ' ' %d\tDelete '\\t' %d\n>>>%s\n\n", len, i, nb, nt, s);
        }
        else // Case 2: not have'\n'
        {
            printf("\n>>>Not have '\\n' in the trail.\n");
            while (s[i - 1] == ' ')
                s[i - 1] = '\0', i--, nb++;
            while (s[i - 1] == '\t')
                s[i - 1] = '\0', i--, nt++;
            if (i == 0)
                printf(">>>Blank, delete.\n");
            else
                printf(">>>Source = %3d\t\tNow = %3d\tDelete ' ' %d\tDelete '\\t' %d\n>>>%s\n\n", len, i, nb, nt, s);
        }
    }

    return 0;
}

int getstr(char s[], int lim)
{
    int c;
    int i;

    i = 0;
    lim = MAXLINE;

    while (i < lim - 1 && (c = getchar()) != EOF && c != '\n')
    {
        s[i] = c;
        i++;
    }
    if (c == '\n')
        s[i] = '\n',
        i++;
    s[i] = '\0';

    return i;
}